package com.team06.listener;
import com.team06.dao.*;
import com.team06.pojo.*;
import com.team06.util.*;
import jakarta.annotation.PostConstruct;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.List;


@Component
public class XmlFileListener implements FileAlterationListener {

    @Autowired
    private OnreDao onreDao;

    @Autowired
    private PokeDao pokeDao;
    @Autowired
    private StlsDao stlsDao;

    @Autowired
    private DfdeDao dfdeDao;

    @Autowired
    private DfdlDao dfdlDao;

    @Autowired
    private DfieDao dfieDao;

    @Autowired
    private DfueDao dfueDao;
    @Autowired
    private DlyeDao dlyeDao;
    @Autowired
    private FettDao fettDao;
    @Autowired
    private FrttDao frttDao;
    @Autowired
    private GtlsDao gtlsDao;
    @Autowired
    private HbttDao hbttDao;


    @Autowired
    private BoreDao boreDao;
    @Autowired
    private CaneDao caneDao;
    @Autowired
    private CfceDao cfceDao;
    @Autowired
    private CkieDao ckieDao;
    @Autowired
    private CklsDao cklsDao;
    @Autowired
    private CkoeDao ckoeDao;
    @Autowired
    private DepeDao depeDao;

    @Autowired
    private CfueDao cfueDao;

    @Autowired
    private AfidDao afidDao;

    @Autowired
    private AirlDao airlDao;

    @Autowired
    private ArreDao arreDao;

    @Autowired
    private BllsDao bllsDao;

    @Autowired
    private BllsBeltDao bllsBeltDao;

    @Autowired
    private ApueDao apueDao;

    @Autowired
    private CfieDao cfieDao;



    @PostConstruct
    public void init() {
        System.out.println("XmlFileListener initialized, messageDao: " + (onreDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, pokeMessageDao: " + (pokeDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, dfueMessageDao: " + (dfueDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, dlyeMessageDao: " + (dlyeDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, fettMessageDao: " + (fettDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, frttMessageDao: " + (frttDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, GtlsMessageDao: " + (gtlsDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, HbttMessageDao: " + (hbttDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, boreDao: " + (boreDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, caneDao: " + (caneDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, cfceDao: " + (cfceDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, ckieDao: " + (ckieDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, cklsDao: " + (cklsDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, ckoeDao: " + (ckoeDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, depeDao: " + (depeDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, apueDao: " + (apueDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, cfueDao: " + (cfueDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, afidDao: " + (afidDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, airlDao: " + (airlDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, arreDao: " + (arreDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, bllsDao: " + (bllsDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, bllsBeltDao: " +(bllsBeltDao != null ? "injected" : "NOT injected"));
        System.out.println("XmlFileListener initialized, cfieDao: " + (cfieDao != null ? "injected" : "NOT injected"));

    }

    @Override
    public void onStart(FileAlterationObserver observer) {
        // 监听开始时执行的操作
    }

    @Override
    public void onDirectoryCreate(File directory) {
        // 目录创建时执行的操作
    }

    @Override
    public void onDirectoryChange(File directory) {
        // 目录变更时执行的操作
    }

    @Override
    public void onDirectoryDelete(File directory) {
        // 目录删除时执行的操作
    }

    @Override
    public void onFileCreate(File file) {
        if (file.getName().endsWith(".xml")) {
            // 区分 onre 和 poke 类型的文件
            if (file.getName().startsWith("DFME-ONRE")) {
                parseAndInsertOnreXml(file);
            } else if (file.getName().startsWith("DFME-POKE")) {
                parseAndInsertPokeXml(file);
            } else if (file.getName().startsWith("DFME-STLS")) {
                parseAndInsertStlsXml(file);
            } else if (file.getName().startsWith("DFOE-DFDE")) {
                parseAndInsertDfdeXml(file);
            } else if (file.getName().startsWith("DFOE-DFDL")) {
                parseAndInsertDfdlXml(file);
            } else if (file.getName().startsWith("DFOE-DFIE")) {
                parseAndInsertDfieXml(file);
            }else if (file.getName().startsWith("DFME-DFUE")) {
                parseAndInsertDfueXml(file);
            }else if (file.getName().startsWith("DFME-DLYE")) {
                parseAndInsertDlyeXml(file);
            }else if (file.getName().startsWith("DFME-FETT")) { // 处理 FETT 类型文件
                parseAndInsertFettXml(file);
            }else if (file.getName().startsWith("DFME-FRTT")) {
                parseAndInsertFrttXml(file);
            }else if (file.getName().startsWith("DFME-GTLS")) {
                parseAndInsertGtlsXml(file);
            }else if (file.getName().startsWith("DFME-HBTT")) {
                parseAndInsertHbttXml(file);
            }else if (file.getName().startsWith("DFME-BORE")) {
                parseAndInsertBoreXml(file);
            } else if (file.getName().startsWith("DFME-CANE")) {
                parseAndInsertCaneXml(file);
            } else if (file.getName().startsWith("DFME-CFCE")) {
                parseAndInsertCfceXml(file);
            } else if (file.getName().startsWith("DFME-CKIE")) {
                parseAndInsertCkieXml(file);
            } else if (file.getName().startsWith("DFME-CKLS")) {
                parseAndInsertCklsXml(file);
            } else if (file.getName().startsWith("DFME-CKOE")) {
                parseAndInsertCkoeXml(file);
            } else if (file.getName().startsWith("DFME-DEPE")) {
                parseAndInsertDepeXml(file);
            } else if (file.getName().startsWith("BASE-APUE")) {
                parseAndInsertApueXml(file);
            } else if (file.getName().startsWith("BASE-CFUE")) {
                parseAndInsertCfueXml(file);
            } else if (file.getName().startsWith("DFME-AFID")) {
                parseAndInsertAfidXml(file);
            } else if (file.getName().startsWith("DFME-AIRL")) {
                parseAndInsertAirlXml(file);
            } else if (file.getName().startsWith("DFME-ARRE")) {
                parseAndInsertArreXml(file);
            } else if (file.getName().startsWith("DFME-BLLS")) {
                parseAndInsertBllsXml(file);
            }  else if (file.getName().startsWith("BASE-CFIE")) {
                parseAndInsertCfieXml(file);
            }
        }
    }

    @Override
    public void onFileChange(File file) {
        if (file.getName().endsWith(".xml")) {
            if (file.getName().startsWith("DFME-ONRE")) {
                parseAndInsertOnreXml(file);
            } else if (file.getName().startsWith("DFME-POKE")) {
                parseAndInsertPokeXml(file);
            } else if (file.getName().startsWith("DFME-STLS")) {
                parseAndInsertStlsXml(file);
            } else if (file.getName().startsWith("DFOE-DFDE")) {
                parseAndInsertDfdeXml(file);
            } else if (file.getName().startsWith("DFOE-DFDL")) {
                parseAndInsertDfdlXml(file);
            } else if (file.getName().startsWith("DFOE-DFIE")) {
                parseAndInsertDfieXml(file);
            }else if (file.getName().startsWith("DFME-DPUE")) {
                parseAndInsertDfueXml(file);
            }else if (file.getName().startsWith("DFME-DLYE")) {
                parseAndInsertDlyeXml(file);
            }else if (file.getName().startsWith("DFME-FETT")) { // 处理 FETT 类型文件
                parseAndInsertFettXml(file);
            }else if (file.getName().startsWith("DFME-FRTT")) {
                parseAndInsertFrttXml(file);
            } else if (file.getName().startsWith("DFME-GTLS")) {
                parseAndInsertGtlsXml(file);
            }else if (file.getName().startsWith("DFME-HBTT")) {
                parseAndInsertHbttXml(file);
            }else if (file.getName().startsWith("DFME-BORE")) {
                parseAndInsertBoreXml(file);
            } else if (file.getName().startsWith("DFME-CANE")) {
                parseAndInsertCaneXml(file);
            } else if (file.getName().startsWith("DFME-CFCE")) {
                parseAndInsertCfceXml(file);
            } else if (file.getName().startsWith("DFME-CKIE")) {
                parseAndInsertCkieXml(file);
            } else if (file.getName().startsWith("DFME-CKLS")) {
                parseAndInsertCklsXml(file);
            } else if (file.getName().startsWith("DFME-CKOE")) {
                parseAndInsertCkoeXml(file);
            } else if (file.getName().startsWith("DFME-DEPE")) {
                parseAndInsertDepeXml(file);
            }else if (file.getName().startsWith("BASE-APUE")) {
                parseAndInsertApueXml(file);
            } else if (file.getName().startsWith("BASE-CFUE")) {
                parseAndInsertCfueXml(file);
            } else if (file.getName().startsWith("DFME-AFID")) {
                parseAndInsertAfidXml(file);
            } else if (file.getName().startsWith("DFME-AIRL")) {
                parseAndInsertAirlXml(file);
            } else if (file.getName().startsWith("DFME-ARRE")) {
                parseAndInsertArreXml(file);
            } else if (file.getName().startsWith("DFME-BLLS")) {
                parseAndInsertBllsXml(file);
            }  else if (file.getName().startsWith("BASE-CFIE")) {
                parseAndInsertCfieXml(file);
            }
        }
    }

    @Override
    public void onFileDelete(File file) {
        if (file.getName().endsWith(".xml")) {
            // 当 XML 文件被删除时执行的操作
        }
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
        // 监听结束时执行的操作
    }

    private void parseAndInsertOnreXml(File file) {
        try {
            // 调用现有的 XML 解析方法
            Onre entity = OnreXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                // 调用插入方法
                onreDao.insertMessage(entity);

            }
        } catch (Exception e) {
            // 记录详细的错误日志
            System.err.println("Failed to parse and insert onre XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertPokeXml(File file) {
        try {
            // 调用现有的 XML 解析方法
            Poke entity = PokeXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                // 调用插入方法
                pokeDao.insertMessage(entity);
            }
        } catch (Exception e) {
            // 记录详细的错误日志
            System.err.println("Failed to parse and insert poke XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertStlsXml(File file) {
        try {
            Stls entity = StlsXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                stlsDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.out.println("Failed to parse and insert STLS XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertDfdeXml(File file) {
        try {
            Dfde entity = DfdeXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                dfdeDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.out.println("Failed to parse and insert DFOE-DFDE XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertDfdlXml(File file) {
        try {
            Dfdl dfdl = DfdlXmlParserUtil.parseXml(file.getAbsolutePath());
            if (dfdl != null) {
                dfdlDao.insertDfdl(dfdl);
            }
        } catch (Exception e) {
            System.out.println("Failed to parse and insert DFOE-DFDL XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertDfieXml(File file) {
        try {
            Dfie dfie = DfieXmlParserUtil.parseXml(file.getAbsolutePath());
            if (dfie != null) {
                dfieDao.insertDfie(dfie);
            }
        } catch (Exception e) {
            System.out.println("Failed to parse and insert DFOE-DFIE XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertDfueXml(File file) {
        try {
            // 调用现有的 XML 解析方法
            Dfue entity = DfueXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                // 调用插入方法
                dfueDao.insertMessage(entity);
            }
        } catch (Exception e) {
            // 记录详细的错误日志
            System.err.println("Failed to parse and insert dfue XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
    private void parseAndInsertDlyeXml(File file) {
        try {
            // 调用现有的 XML 解析方法
            Dlye entity = DlyeXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                // 调用插入方法
                dlyeDao.insertMessage(entity);
            }
        } catch (Exception e) {
            // 记录详细的错误日志
            System.err.println("Failed to parse and insert dlye XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
    private void parseAndInsertFettXml(File file) {
        try {
            Fett entity = FettXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                fettDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert dlye XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
    private void parseAndInsertFrttXml(File file) {
        try {
            // 调用现有的 XML 解析方法
            Frtt entity = FrttXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                // 调用插入方法
                frttDao.insertMessage(entity);
            }
        } catch (Exception e) {
            // 记录详细的错误日志
            System.err.println("Failed to parse and insert frt XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
    private void parseAndInsertGtlsXml(File file) {
        try {
            Gtls entity = GtlsXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                gtlsDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert frt XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
    private void parseAndInsertHbttXml(File file) {
        try {
            // 调用现有的 XML 解析方法
            Hbtt entity = HbttXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                // 调用插入方法
                hbttDao.insertMessage(entity);
            }
        } catch (Exception e) {
            // 记录详细的错误日志
            System.err.println("Failed to parse and insert hbtt XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
    private void parseAndInsertBoreXml(File file) {
        try {
            Bore entity = BoreXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                boreDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert bore XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertCaneXml(File file) {
        try {
            Cane entity = com.team06.util.CaneXmlParserUtil.parseXml(file);
            if (entity != null) {
                caneDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert cane XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertCfceXml(File file) {
        try {
            Cfce entity = com.team06.util.CfceXmlParserUtil.parseXml(file);
            if (entity != null) {
                cfceDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert cfce XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertCkieXml(File file) {
        try {
            Ckie entity = com.team06.util.CkieXmlParserUtil.parseXml(file);
            if (entity != null) {
                ckieDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert ckie XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertCklsXml(File file) {
        try {
            Ckls entity = com.team06.util.CklsXmlParserUtil.parseXml(file);
            if (entity != null) {
                cklsDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert ckls XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertCkoeXml(File file) {
        try {
            Ckoe entity = com.team06.util.CkoeXmlParserUtil.parseXml(file);
            if (entity != null) {
                ckoeDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert ckoe XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertDepeXml(File file) {
        try {
            Depe entity = com.team06.util.DepeXmlParserUtil.parseXml(file);
            if (entity != null) {
                depeDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert depe XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
    private void parseAndInsertApueXml(File file) {
        try {
            Apue entity = ApueXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                apueDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert apue XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertCfueXml(File file) {
        try {
            Cfue entity = CfueXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                cfueDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert cfue XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertAfidXml(File file) {
        try {
            Afid entity = AfidXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                afidDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert afid XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertAirlXml(File file) {
        try {
            Airl entity = AirlXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                airlDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert airl XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertArreXml(File file) {
        try {
            Arre entity = ArreXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                arreDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert arre XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertBllsXml(File file) {
        try {
            // 解析并插入 MSG_DFME_BLLS 表数据
            Blls bllsEntity = BllsXmlParserUtil.parseXml(file.getAbsolutePath());
            if (bllsEntity != null) {
                bllsDao.insertMessage(bllsEntity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert blls XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }

        try {
            // 解析并插入 DFME_BLLS_BELT 表数据
            List<BllsBelt> belts = BllsXmlParserUtil.parseBllsBelts(file.getAbsolutePath());
            if (belts != null && !belts.isEmpty()) {
                bllsBeltDao.insertBelts(belts); // 这里就是你写的 DAO 方法
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert BELT data from blls XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }

    private void parseAndInsertCfieXml(File file) {
        try {
            Cfie entity = CfieXmlParserUtil.parseXml(file.getAbsolutePath());
            if (entity != null) {
                cfieDao.insertMessage(entity);
            }
        } catch (Exception e) {
            System.err.println("Failed to parse and insert cfie XML file: " + file.getAbsolutePath());
            e.printStackTrace();
        }
    }
}





